//
//  packButtonViewController.m
//  Journey
//
//  Created by dllo on 16/5/20.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "packButtonViewController.h"
#import "SSNetworking.h"
#import "packModel.h"
#import "nameModel.h"
#import "guidesModel.h"
#import "packCollectionViewCell.h"
#import "packTableViewCell.h"
static NSString *const packButtonCell = @"packButtonCELL";
static NSString *const packCollectionCell = @"packCollectionCELL";
@interface packButtonViewController ()<UITableViewDataSource,UITableViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate>
@property(nonatomic,strong)UITableView *packTableView;
@property(nonatomic,strong)UICollectionView *packCollectionView;
@property(nonatomic,copy)NSString *myId;
@property(nonatomic,strong)NSMutableArray *packTArray;
@property(nonatomic,strong)NSMutableArray *coverArray;
@end

@implementation packButtonViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"看锦囊";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"return"] style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonAction)];
   // self.nameArray = [NSMutableArray array];
    self.packTArray = [NSMutableArray array];
    self.coverArray = [NSMutableArray array];
    [self getData];
    [self getFirstData];
    self.packTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width / 4, self.view.frame.size.height) style:UITableViewStylePlain];
    [self.view addSubview:self.packTableView];
    self.packTableView.delegate = self;
    self.packTableView.dataSource = self;
    [self.packTableView registerClass:[packTableViewCell class] forCellReuseIdentifier:packButtonCell];
    self.packTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(130 * WIDTHC, 200 * WIDTHC);
    flowLayout.minimumLineSpacing = 10;
     flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.packCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(self.packTableView.frame.size.width + 20, 84 * WIDTHC, self.view.frame.size.width - self.packTableView.frame.size.width - 40, self.view.frame.size.height - 64) collectionViewLayout:flowLayout];
    self.packCollectionView.backgroundColor = [UIColor whiteColor];
    self.packCollectionView.delegate = self;
    self.packCollectionView.dataSource = self;
    [self.packCollectionView registerClass:[packCollectionViewCell class] forCellWithReuseIdentifier:packCollectionCell];
    [self.view addSubview:self.packCollectionView];
//    [self getCollectionViewData];

    
}




-(void)getFirstData{
    NSString *url = @"http://open.qyer.com/qyer/guide/channel_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&id=999999&page=1&track_app_channel=App%2520Store&track_app_version=6.9.0&track_device_info=iPhone%25205s&track_deviceid=E3EBA485-00FA-421D-BACB-D791AF3400E2&track_os=ios%25209.3.1&v=1";
    [SSNetworking ssGETWithURL:url parameter:nil success:^(id result) {
        
        NSDictionary *dic = result;
        NSMutableArray *dataArray = [dic objectForKey:@"data"];
        
        for (NSDictionary *dataDic in dataArray) {
            
            nameModel *model = [[nameModel alloc] init];
            
            [model setValuesForKeysWithDictionary:dataDic];
            
            //     self.coverArray = [NSMutableArray arrayWithArray:model.guides];
            
            [self.coverArray  addObjectsFromArray:model.guides];
            
        }
        [self.packCollectionView reloadData];
        
        
    } failure:^(NSError *error) {
        
        
        
    } network:^(BOOL networking) {
        
        
    }];
    

    
}





-(void)getCollectionViewData{
    
    NSString *str = @"%2520";
    NSString *str1 = @"%25205";
    NSString *str2 = @"%25209.3.1";
  
        NSString *url = [NSString stringWithFormat:@"http://open.qyer.com/qyer/guide/channel_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&id=%@&page=1&track_app_channel=App%@Store&track_app_version=6.9.0&track_device_info=iPhone%@s&track_deviceid=E3EBA485-00FA-421D-BACB-D791AF3400E2&track_os=ios%@&v=1",self.myId,str,str1,str2];
  //  NSLog(@"====%@",url);
        
    [SSNetworking ssGETWithURL:url parameter:nil success:^(id result) {
        
        NSDictionary *dic = result;
        NSMutableArray *dataArray = [dic objectForKey:@"data"];
        
        for (NSDictionary *dataDic in dataArray) {
            
            nameModel *model = [[nameModel alloc] init];
            
            [model setValuesForKeysWithDictionary:dataDic];
            
       //     self.coverArray = [NSMutableArray arrayWithArray:model.guides];
            
            [self.coverArray  addObjectsFromArray:model.guides];
            
           }
         [self.packCollectionView reloadData];

        
    } failure:^(NSError *error) {
        
        
        
    } network:^(BOOL networking) {
        
        
    }];
    
}

-(void)getData{

          [SSNetworking ssGETWithURL:@"http://open.qyer.com/qyer/guide/category_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&oauth_token=aa2b598eae24c66826dc4e4ba1cacc76&page=1&track_app_channel=App%2520Store&track_app_version=6.9.0&track_device_info=iPhone%25205s&track_deviceid=E3EBA485-00FA-421D-BACB-D791AF3400E2&track_os=ios%25209.3.1&track_user_id=7907956&v=1" parameter:nil success:^(id result) {
              
              NSDictionary *dic = result;
              NSMutableArray *dataArray = [dic objectForKey:@"data"];
              
              for (NSDictionary *dataDic in dataArray) {
                  
                  packModel *pack = [[packModel alloc] init];
                  [pack setValuesForKeysWithDictionary:dataDic];
                  [self.packTArray addObject:pack];
                  
              }
              [self.packTableView reloadData];
              //   默认点击第一个cell
              [self.packTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone];
              
//              packModel *model1 = [self.packTArray objectAtIndex:0];
//              
//              self.myId = model1.myId;
            
//
//              //右边的collectionview也走第一个cell
//              nameModel *model = [self.coverArray objectAtIndex:0];
//              self.coverArray = [NSMutableArray arrayWithArray:model.guides];
//
//              [self.coverArray addObjectsFromArray:model.guides];
//              
//              [self.packCollectionView reloadData];
     
          } failure:^(NSError *error) {
              
              
          } network:^(BOOL networking) {
              
              NSLog(@"没有网");
          }];
    
          
}


//collection
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
//    NSLog(@"count ==== %lu",(unsigned long)self.coverArray.count);
    return self.coverArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    packCollectionViewCell *item = [collectionView dequeueReusableCellWithReuseIdentifier:packCollectionCell forIndexPath:indexPath];
    guidesModel *guid = [self.coverArray objectAtIndex:indexPath.row];
   
 //   NSLog(@"照片 == %@",guid.cover);
    
   // item.backgroundColor = [UIColor yellowColor];
    item.guides = guid;
 
    return item;
}

/*
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
     guidesModel *guid = [self.coverArray objectAtIndex:indexPath.row];
    
    NSString *urlString = guid.file;
    NSURL *url = [NSURL URLWithString:urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    
    
    
}
*/

//tableView
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.packTArray.count;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    packTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:packButtonCell];
    
 //   NSLog(@"====%@",self.packTArray);
    packModel *model = [self.packTArray objectAtIndex:indexPath.row];
    cell.textLabel.text = model.cnname;
    cell.textLabel.textColor = [UIColor blackColor];
    cell.textLabel.highlightedTextColor = [UIColor redColor];
    
    return cell;
}

//tableview点击方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    packModel *model = [self.packTArray objectAtIndex:indexPath.row];
    self.myId = model.myId;
    
    
    [self.coverArray removeAllObjects];
    
   // NSLog(@"id = %@",self.myId);
    [self getCollectionViewData];
   [self.packCollectionView reloadData];
    
    
}


-(void)leftButtonAction{
    [self dismissViewControllerAnimated:YES completion:^{
    }];
}
    



@end
